//
//  ProvincesPicker.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/11/8.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "ProvincesPicker.h"

@interface ProvincesPicker()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *provincepicker;
@property (nonatomic,strong) NSMutableArray *provinces;//省份数组
@property (nonatomic,assign) NSInteger proIndex;//选中省份索引
@property (nonatomic,assign) NSInteger cityIndex;//选中城市索引
@property (nonatomic,assign) NSInteger areaIndex;//选中城市索引

@end

@implementation ProvincesPicker
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.provincepicker.delegate = self;
}
- (IBAction)sureClick:(UIButton *)sender
{
    NSMutableString *addressStr = [NSMutableString string];
    ConProvincesModel *proModel = self.provinces[self.proIndex];
    ConCitiesModel *cityModel = proModel.cities[self.cityIndex];
    [addressStr appendString:proModel.name];
    [addressStr appendString:cityModel.name];
    [addressStr appendString:cityModel.areas[self.areaIndex]];
    if (self.selectModel) {
        self.selectModel(addressStr);
    }
}
+ (instancetype)picker
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([ProvincesPicker class]) owner:nil options:nil]lastObject];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.provinces.count;
    }else if (component == 1){
        ConProvincesModel *proModel = self.provinces[self.proIndex];
        return proModel.cities.count;
    }
    ConProvincesModel *proModel = self.provinces[self.proIndex];
    ConCitiesModel *cityModel = proModel.cities[self.cityIndex];
    return cityModel.areas.count;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        ConProvincesModel *model = self.provinces[row];
        return model.name;
    }else if (component == 1){
        ConProvincesModel *proModel = self.provinces[self.proIndex];
        ConCitiesModel *cityModel = proModel.cities[row];
        return cityModel.name;
    }
    ConProvincesModel *proModel = self.provinces[self.proIndex];
    ConCitiesModel *cityModel = proModel.cities[self.cityIndex];
    return cityModel.areas[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.font = Font(16);
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.proIndex = row;
        self.cityIndex = 0;
        self.areaIndex = 0;
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }else if (component == 1){
        self.cityIndex = row;
        self.areaIndex = 0;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }else{
        self.areaIndex = row;
    }
}
- (NSMutableArray *)provinces
{
    if (!_provinces) {
        _provinces = [NSMutableArray array];
        NSDictionary *cityDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"address" ofType:@"plist"]];
        NSArray *cityArr = cityDict[@"address"];
        for (NSInteger i=0; i<cityArr.count; i++) {
            ConProvincesModel *model = [ConProvincesModel objectWithDict:cityArr[i]];
            [_provinces addObject:model];
        }
    }
    return _provinces;
}
@end
